package com.mzy.interview

/**
 * @Author: mizy3
 * @Date: 2024/08/25/下午12:31
 * @Description:
 */
class 反转链表II {
    fun reverseBetween(head: ListNode?, left: Int, right: Int): ListNode? {
        if (head == null) return null
        var tmp: Int = left
        var cur = head
        var dummy: ListNode? = ListNode(-1, head)
        var p = dummy
        while (tmp-- > 1) {
            p = p?.next
            cur = cur?.next
        }

        var helper = right - left
        while (helper-- > 0){
            val removed = cur?.next
            cur?.next = cur?.next?.next
            removed?.next = p?.next
            p?.next = removed
        }
        return dummy?.next
    }
}

fun main() {
    val lit = ListNode(1, ListNode(2, ListNode(3, ListNode(4,ListNode(5)))))
    反转链表II().reverseBetween(lit, 2, 4)
}